﻿using JTG.ServiceInterface.Attribute;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Tzw.Common;

namespace JTG.ServiceInterface.InterfaceService
{
    /// <summary>
    /// his接口桥接，德阳人民医院
    /// </summary>
    [FunctionName(FunctionType.德阳人民医院HIS)]
    public class HisBridgeService : IHisBridgeService
    {
        /// <summary>
        /// 签到
        /// </summary>
        /// <returns></returns>
        public string Request(string url, string xmlTxt, params string[] args)
        {
            try
            {
                return SoapHelper.GetSoapReSource(url, xmlTxt);
            }
            catch (Exception e)
            {
                File.AppendAllText("error.log", $"错误信息：{e.Message},{e.StackTrace}");
                return "";
            }
        }
    }

    /// <summary>
    /// his接口桥接，德阳人民医院
    /// </summary>
    [FunctionName(FunctionType.德阳人民医院HIS_SOAP)]
    public class HisBridgeServiceSoap : IHisBridgeService
    {
        public string Request(string url, string xmlTxt, params string[] args)
        {
            try
            {
                var userName = ConfigurationManager.AppSettings["UserName"];
                var pwd = ConfigurationManager.AppSettings["Password"];

                //反射方式调用
                var hipService = new WebServiceHelper($"{args[0]}.dll", url, args[0] /*"HIPService"*/,new List<string>() {args[1] /*"HIPMessageServer"*/, "Dispose" });
                //得到需要赋值的字段类
                var ap = hipService.m_ObjInvoke.GetType().GetProperty("AuthenticationValue")?.PropertyType;
                //实例化类
                var instance = Activator.CreateInstance(ap);
                //赋值
                instance.GetType().GetProperty("appsys_id")?.SetValue(instance, userName, null);
                instance.GetType().GetProperty("appsys_token")?.SetValue(instance, pwd, null);

                //调用服务
                hipService.m_ObjInvoke.GetType().GetProperty("AuthenticationValue")?.SetValue(hipService.m_ObjInvoke, instance, null);

                var result = hipService.InvokeMethod(args[1] /*"HIPMessageServer"*/, args[2] /*"DocumentAccess"*/, xmlTxt).ToString();
                
                //
                hipService.InvokeMethod("Dispose");

                return result;
            }
            catch (Exception e)
            {
                File.AppendAllText("error.log", $"错误信息：{e.Message},{e.StackTrace}");
                return "";
            }
        }
    }
}